<?php
/**
 * 生成google专用ical格式
 */
class _interface extends __interface {
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see __interface::dispose_data()
	 */
	public function dispose_data() {
		global $cache;
		// 获取要显示开开始和结束日期
		$this->day_begin = strtotime ( date ( "Y-m-d" ) );
		$this->day_end = strtotime ( date ( "Y-m-d" ) );
		
		$s = "BEGIN:VCALENDAR\r\n";
		$s .= "PRODID:-//mdeve.com//天气、农历、节日//CN\r\n";
		$s .= "VERSION:2.0\r\n";
		$s .= "METHOD:PUBLISH\r\n";
		$s .= "X-CALSTART:" . date ( "Ymd", $this->day_begin ) . "T000000\r\n";
		$s .= "X-CALEND:" . date ( "Ymd", dateadd ( 'day', 3, $this->day_begin ) ) . "T000000\r\n";
		$s .= "X-WR-CALNAME:" . $this->weather_data['city_2']."-".$this->weather_data['city_1'] . "天气预报\r\n";
		$s .= "X-APPLE-CALENDAR-COLOR:#990000\r\n";
		$s .= "X-WR-TIMEZONE:Asia/Shanghai\r\n";
		
		// 如果要显示天气
		$weather_data = array ();
	if ($this->style ["show_weather"]) {
			// 处理今天天气
			$date = date("Y-").$this->weather_data['update_time']["month"]."-".$this->weather_data['update_time']["day"];
			$date = strtotime ( $date );
			
			$detail = "";
			// 实时温度信息
			$detail = "";
			if(array_key_exists("realtime", $this->weather_data)){
				$detail .= "实时天气：" . $this->weather_data ['realtime'] ['weather'] . "\\n";
				$detail .= "实时温度：" . $this->weather_data ['realtime'] ['tempn'] . "\\n";
				$detail .= "天气网发布时间：" . date("Y"). "-". $this->weather_data['update_time']['month']. "-". $this->weather_data ['update_time'] ['day']. "\\n";
				$detail .= "更新时间：" . $this->weather_data ['lastupdate'] . "\\n";
			}
			// AQI信息
			if(array_key_exists("air_aqi", $this->weather_data)){
				$detail .= "\\n---------- 空气质量指数 ----------\\n";
				if($this->weather_data["air_aqi"]<=50) $aqi_level = "优";
				if($this->weather_data["air_aqi"]>50 && $this->weather_data["air_aqi"]<=100) $aqi_level = "良";
				if($this->weather_data["air_aqi"]>100 && $this->weather_data["air_aqi"]<=150) $aqi_level = "轻度污染";
				if($this->weather_data["air_aqi"]>150 && $this->weather_data["air_aqi"]<=200) $aqi_level = "中度污染";
				if($this->weather_data["air_aqi"]>200 && $this->weather_data["air_aqi"]<=300) $aqi_level = "重度污染";
				if($this->weather_data["air_aqi"]>300) $aqi_level = "严重污染";
				$detail .= "空气质量：{$aqi_level}  AQI：" . $this->weather_data["air_aqi"] . "\\n";
			}
			// 天气详细情况
			$detail .= "\\n---------- 天气详细情况 ----------\\n";
			$detail .= $this->weather_data["today"]["title_1"].$this->weather_data["today"]["weather_1"]. " ";
			$detail .= $this->weather_data["today"]["wind_direction_1"].$this->weather_data["today"]["wind_power_1"]. "\\n";
			$detail .= $this->weather_data["today"]["title_2"].$this->weather_data["today"]["weather_2"]. " ";
			$detail .= $this->weather_data["today"]["wind_direction_2"].$this->weather_data["today"]["wind_power_2"]. "\\n";
			$detail .= "温度：". $this->weather_data["today"]["temperature_1"]. "℃~". $this->weather_data["today"]["temperature_2"]. "℃\\n";
			$detail .= $this->weather_data["today"]["sun_1"]. " ". $this->weather_data["today"]["sun_1"] . "\\n";
			
			// 通知栏显示标题
			$title = $this->weather_data["today"]["weather_1"]. " ";
			$title .= $this->weather_data["today"]["temperature_1"]. "℃ ";
			$title .= $this->weather_data["today"]["wind_direction_1"].$this->weather_data["today"]["wind_power_1"]. " ";
			// 建立日历条目
			$s .= $this->create_vevent($date, $title, $detail);
		
			// 明天气详细情况
			$detail = "\\n---------- 天气详细情况 ----------\\n";
			$detail .= "明天白天：". $this->weather_data["tomorrow"]["weather_day"]. "\\n";
			$detail .= "最高温度：". $this->weather_data["tomorrow"]["temperature_day"]. "℃\\n";
			$detail .= "风向：". $this->weather_data["tomorrow"]["wind_direction_day"].$this->weather_data["tomorrow"]["wind_power_day"]. "\\n";
			$detail .= "明天晚上：". $this->weather_data["tomorrow"]["weather_night"]. "\\n";
			$detail .= "最低温度：". $this->weather_data["tomorrow"]["temperature_night"]. "℃\\n";
			$detail .= "风向：". $this->weather_data["tomorrow"]["wind_direction_night"].$this->weather_data["tomorrow"]["wind_power_night"]. "\\n";
			$detail .= $this->weather_data["tomorrow"]["sun_up"]. " ". $this->weather_data["tomorrow"]["sun_down"] . "\\n";
			// 通知栏显示标题
			$title = $this->weather_data["tomorrow"]["weather_day"]. "~". $this->weather_data["tomorrow"]["weather_night"]. " ";
			$title .= $this->weather_data["tomorrow"]["temperature_day"]. "℃~". $this->weather_data["tomorrow"]["temperature_night"];
			// 建立日历条目
			$s .= $this->create_vevent($date+86400, $title, $detail);


			// 后天天气详细情况
			$detail = "\\n---------- 天气详细情况 ----------\\n";
			$detail .= "明天白天：". $this->weather_data["after_tomorrow"]["weather_day"]. "\\n";
			$detail .= "最高温度：". $this->weather_data["after_tomorrow"]["temperature_day"]. "℃\\n";
			$detail .= "风向：". $this->weather_data["after_tomorrow"]["wind_direction_day"].$this->weather_data["after_tomorrow"]["wind_power_day"]. "\\n";
			$detail .= "明天晚上：". $this->weather_data["after_tomorrow"]["weather_night"]. "\\n";
			$detail .= "最低温度：". $this->weather_data["after_tomorrow"]["temperature_night"]. "℃\\n";
			$detail .= "风向：". $this->weather_data["after_tomorrow"]["wind_direction_night"].$this->weather_data["after_tomorrow"]["wind_power_night"]. "\\n";
			$detail .= $this->weather_data["after_tomorrow"]["sun_up"]. " ". $this->weather_data["after_tomorrow"]["sun_down"] . "\\n";
			// 通知栏显示标题
			$title = $this->weather_data["after_tomorrow"]["weather_day"]. "~". $this->weather_data["after_tomorrow"]["weather_night"]. " ";
			$title .= $this->weather_data["after_tomorrow"]["temperature_day"]. "℃~". $this->weather_data["after_tomorrow"]["temperature_night"];
			// 建立日历条目
			$s .= $this->create_vevent($date+86400*2, $title, $detail);
			
			// 天气预警
			if(sizeof($this->weather_data["alarm"])){
				foreach ($this->weather_data["alarm"] as $r){
					$title = $r["w1"]. $r["w2"]. $r["w5"]. $r["w7"]. "预警";
					$detail = $r["w9"];
					// 建立日历条目
					$s .= $this->create_vevent($date, $title, $detail);
				}
			}
			
		}
		$s .= "END:VCALENDAR";
		header ( "Content-Type: text/calendar; charset=gb2312" );
		echo iconv ( "utf-8", "gb2312", $s );
	}
	
	
	private function create_vevent($date, $title, $detail){
		$s = "BEGIN:VEVENT\r\n";
		$s .= "CLASS:PUBLIC\r\n";
		$s .= "CREATED:" . date ( "Ymd" ) . "T" . date ( "His" ) . "Z\r\n";
		$s .= "DESCRIPTION:{$detail} \r\n";
		$s .= "DTSTART;TZID=Asia/Chongqing:" . date ( "Ymd", $date ) . "T" . date ( "Hi00" ) . "\r\n";
		$s .= "DTEND;TZID=Asia/Chongqing:" . date ( "Ymd", $date ) . "T235959" . "\r\n";
		$s .= "DTSTAMP:" . date ( "Ymd", dateadd ( 'day', - 1, $date ) ) . "T" . date ( "His" ) . "Z\r\n";
		$s .= "LAST-MODIFIED:" . date ( "Ymd" ) . "T" . date ( "His" ) . "Z\r\n";
		$s .= "LOCATION:" . $this->weather_data ["city_1"] . "\r\n";
		$s .= "SEQUENCE:0\r\n";
		$s .= "PRIORITY:6\r\n";
		$s .= "SUMMARY:{$title}\r\n";
		$s .= "TRANSP:TRANSPARENT\r\n";
		$s .= "UID:" . $this->create_uid () . "\r\n";
		$s .= "STATUS:TENTATIVE\r\n";
		$s .= "CATEGORIES:FREE\r\n";
		$s .= "END:VEVENT\r\n";
	
		return $s;
	}
	
	private function create_uid() {
		return strtoupper ( $this->randomstr ( 8 ) . "-" . $this->randomstr ( 4 ) . "-" . $this->randomstr ( 4 ) . "-" . $this->randomstr ( 4 ) . "-" . $this->randomstr ( 12 ) );
	}
	private function randomstr($length, $numeric = 0) {
		mt_srand ( ( double ) microtime () * 1000000 );
		if ($numeric) {
			$hash = sprintf ( '%0' . $length . 'd', mt_rand ( 0, pow ( 10, $length ) - 1 ) );
		} else {
			$hash = '';
			$chars = 'ABCDEF0123456789';
			$max = strlen ( $chars ) - 1;
			for($i = 0; $i < $length; $i ++) {
				$hash .= $chars [mt_rand ( 0, $max )];
			}
		}
		return $hash;
	}
}